package com.vcredit.creditcat.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.vcredit.creditcat.framework.pagination.Page;
import com.vcredit.creditcat.model.Banner;
import com.vcredit.creditcat.service.BannerService;
import com.vcredit.creditcat.util.JsonUtil;
import com.vcredit.creditcat.vo.BannerVo;

/*
 * @author zhangxingong
 * @version 1.0
 * @since 1.0
*/
@RestController
@RequestMapping("/banner")
public class BannerController extends BaseController {

	@Autowired
	private BannerService bannerService;

	@RequestMapping("/list")
	public ModelAndView showPageBanner(HttpServletRequest request) {
		return new ModelAndView("/banner/bannerList");
	}

	@RequestMapping("/listBanner")
	public Page<Banner> findByPageBanner(HttpServletRequest request) {
		Page<Banner> banner = bannerService.findByPage(makeDefaultCriteria(request));
		return banner;
	}

	@RequestMapping("/saveBanner")
	public String saveBanner(@RequestBody BannerVo bannerVo, HttpServletRequest request) {
		bannerService.save(bannerVo);
		return SUCCESS;
	}

	@RequestMapping("/updateBanner")
	public String bannerUpdate(@RequestBody Banner banner, HttpServletRequest request) {
		if (banner.getBannerId() != null) {
			bannerService.update(banner);
		} else {
			bannerService.saveBanner(banner);
		}

		return SUCCESS;
	}

	@RequestMapping("/removeBanner")
	public String removeBanner(@RequestParam("ids") String ids, HttpServletRequest request) {
		bannerService.removeBanner(ids);
		return SUCCESS;
	}

	@RequestMapping("/uploadFile")
	public String uploadFile(HttpServletRequest request) throws JsonProcessingException {
		Map<String, Object> file = null;
		try {
			file = bannerService.upladImage(request);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return JsonUtil.obj2Json(file, true);
	}

}
